  let G = {};
  G.next = $('#next');
  G.email = $('#email');
  G.user = $('#user');
  G.vcode_btn = $('#vcode-btn');
  G.ud_btn = $('#ud-btn');
  G.input = $('.main input');
  G.timer = null;
  G.seconds = 120;
  G.input.keydown(function(){
    $(this).removeClass('err')
  });
  function checkemail() {
    if(!G.user.val().trim().length){
      G.user.focus();
      G.user.addClass('err');
      return false
    }
    if (!/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(G.email.val())) {
      G.email.focus();
      G.email.addClass('err');
      return false
    }
    let {isok} = $.ajax({
        url: `ajax/check.php?type=3&user=${G.user.val()}&email=${G.email.val()}`,
        type: 'GET',
        dataType: 'json',
        async:false
    }).responseJSON;
    if(isok){
      G.user.removeClass('err');
      G.email.removeClass('err');
      return true
    }else{
      G.user.addClass('err');
      G.email.addClass('err');
      return false
    }
    
  }
  G.vcode_btn.click(function(){
    let time_c = $('#time-container');
    let time_n = $('#time-num');
    if(checkemail()){
    //   G.email.focus(function(){
    //     $(this).blur()
    //   })
      time_c.show();
      if(!G.timer){
        // 发送邮件Ajax请求
        $.ajax({
          url: 'ajax/vcode.php',
          type: 'POST',
          dataType: 'json',
          data: {user:G.user.val().trim(),email:G.email.val().trim()},
        })
        .done(function(data){
          if(!data.success){
            $('#msg').html('<div class="err-net"><span style="vertical-align:text-top">网络波动较大,请稍后再试</span></div>');
          }
        });
        // 计时器
        $(this).addClass('undo');
        time_n.text(G.seconds);
        let t = G.seconds;
        G.timer = setInterval(() => {
          t -= 1;
          if(t < 1){
            $(this).removeClass('undo');
            time_c.hide();
            clearInterval(G.timer);
            G.timer = null
          }
          time_n.text(t);
        },1000);
      }
    }

  });
  G.ud_btn.click(function(e){
      e ? e.preventDefault() : window.e.returnValue = false;
    if(checkemail()){
      let vcode = $('#vcode');
      let pwd = $('#pwd');
      let pwd2 = $('#pwd2');
      let tip = $('#tip');
      if (vcode.val() === '') {
        vcode.focus();
        vcode.addClass('err');
      }else if(pwd.val().length < 8){
        pwd.focus();
        pwd.addClass('err')
      }else if(pwd2.val() !== pwd.val()){
        pwd2.focus();
        pwd2.addClass('err')
      }else{
       $.ajax({
          url: 'ajax/postpwd.php',
          type: 'POST',
          dataType: 'json',
          data: {user:G.user.val().trim(),vcode:vcode.val().trim(),pwd:pwd.val()},
        })
        .done(function(data){
            tip.text(data.msg)
            tip.css('top','60px')
            setTimeout(function(){
                tip.css('top','-5px')
                if(data.success){
                    setTimeout(function(){
                        window.location.href = "login.php"
                    },500)
                }
            },1000)
        });
      }
    }
  });

